//火球移动方法
function fireBall(PERSON,personName,FIREBALLARRAY){
	ballNextPoint = new Object();
	var floodToFireImg;
	ballNextPoint.index = FIREBALLARRAY.length;
	ballNextPoint.x=PERSON.x;
	ballNextPoint.y=PERSON.y;
	if(PERSON.name==personName + 'l'){
		ballNextPoint.direction = 'l';
		ballNextPoint.y -= 1;
	}

	if(PERSON.name==personName + 't'){
		ballNextPoint.direction = 't';
		ballNextPoint.x -= 1;
	}
	if(PERSON.name==personName + 'r'){
		ballNextPoint.direction = 'r';
		ballNextPoint.y += 1;
	}
	if(PERSON.name==personName){
		ballNextPoint.direction = 'd';
		ballNextPoint.x += 1;
	}
	//炮弹下一个坐标值
	var nextIndex =maps[gameLevel][ballNextPoint.x][ballNextPoint.y];
	//炮弹下一个坐标标签
	floodToFireImg = getByXY(ballNextPoint.x,ballNextPoint.y);
	//判断是否为地板
	if(nextIndex==1||nextIndex==2){
		//将炮弹对象添加到对应炮弹数组
		FIREBALLARRAY[FIREBALLARRAY.length] = ballNextPoint;
		//将地板换成炮弹
		floodToFireImg.src ="img/gameViewIMG/fireBall.jpg";
		//把地板值换成炮弹值
		maps[gameLevel][ballNextPoint.x][ballNextPoint.y] = 5;
		//进入炮弹移动方法
		fireBallMove(PERSON,ballNextPoint,FIREBALLARRAY);
	}else if(nextIndex==9){
		if(FIREBALLARRAY ==monsterFireBallArray){
			HERO.Blood--;
			setINFO(HERO.Blood,gameLevel,monsterArray.length);
			if(HERO.Blood==0){
				resetConfim();
			}
		}
	}else if(nextIndex==8){
		if(FIREBALLARRAY == userFireBallArray){
			toNextLevel(ballNextPoint);
		}
	}
}
function fireBallMove(PERSON,BALL,FIREBALLARRAY){
	
	var forNextPoint = new Object();
	if(BALL.direction == 'l'){
		BALL.interval = setInterval(function(){
		forNextPoint.x = BALL.x;
		forNextPoint.y = BALL.y-1;
		fireBallMoveChangImg(PERSON,BALL,forNextPoint,FIREBALLARRAY);
		},PERSON.FireSpeed);
	}
	if(BALL.direction == 'r'){
		BALL.interval = setInterval(function(){
		forNextPoint.x = BALL.x;
		forNextPoint.y = BALL.y+1;
		fireBallMoveChangImg(PERSON,BALL,forNextPoint,FIREBALLARRAY);
		},PERSON.FireSpeed);
	}
	if(BALL.direction == 't'){
		BALL.interval = setInterval(function(){
		forNextPoint.x = BALL.x-1;
		forNextPoint.y = BALL.y;
		fireBallMoveChangImg(PERSON,BALL,forNextPoint,FIREBALLARRAY);
		},PERSON.FireSpeed);
	}
	if(BALL.direction == 'd'){
		BALL.interval = setInterval(function(){
		forNextPoint.x = BALL.x+1;
		forNextPoint.y = BALL.y;
		fireBallMoveChangImg(PERSON,BALL,forNextPoint,FIREBALLARRAY);
		},PERSON.FireSpeed);
	}
}
function fireBallMoveChangImg(PERSON,BALL,forNextPoint,FIREBALLARRAY){
		var ball = getByXY(BALL.x,BALL.y);
		var flood = getByXY(forNextPoint.x,forNextPoint.y);
		var floodIndex = maps[gameLevel][forNextPoint.x][forNextPoint.y];
		var floodsrc = flood.src;
		maps[gameLevel][BALL.x][BALL.y] = 2;
		if(floodIndex==3||floodIndex==4){
			clearInterval(BALL.interval);
			ball.src="img/gameViewIMG/floodBoom.jpg";
			setTimeout(function(){
				ball.src="img/gameViewIMG/flood2.jpg";
			},50);
		}else if(floodIndex==9){
			if(FIREBALLARRAY == monsterFireBallArray){
				clearInterval(BALL.interval);
				ball.src="img/gameViewIMG/flood2.jpg";
				HERO.Blood--;
				setINFO(HERO.Blood,gameLevel,monsterArray.length);
				if(HERO.Blood==0){
					resetConfim();
				}
			}
		}else if(floodIndex==8){
			if(FIREBALLARRAY == monsterFireBallArray){
				clearInterval(BALL.interval);
				ball.src="img/gameViewIMG/flood2.jpg";
				BALL.y = forNextPoint.y;
				BALL.x= forNextPoint.x;
			}else if(FIREBALLARRAY == userFireBallArray){
				clearInterval(BALL.interval);
				//把球对象变为地板图片并且怪物血量-1
				ball.src="img/gameViewIMG/flood2.jpg";
				toNextLevel(forNextPoint);
			}
		}else{
			maps[gameLevel][forNextPoint.x][forNextPoint.y] = 5;
			flood.src="img/gameViewIMG/fireBall.jpg";
			BALL.y = forNextPoint.y;
			BALL.x= forNextPoint.x;
			ball.src="img/gameViewIMG/flood2.jpg";
		}
}

function resetConfim(){
		clearAll();
		if(confirm("失敗,是否重新玩!!")){
			HERO.Blood = HERO.initBlood;
			HERO.name = heroName;
			initMap();
			document.getElementById("mainView").style.display="none";
			setMainMap(maps[gameLevel],heroName);
		}else{
			initMap();
			document.getElementById("mainView").style.display="none";
			INFODIV.style.display="none";
			$("#startView").slideDown(500);
			selectHeroVideo.pause();
			startVideo.play();
		}
}

function toNextLevel(forNextPoint){
	var shootMonster;
	
	//得到被射击怪物对象
	for(var i = 0;i<monsterArray.length;i++)
		if(forNextPoint.y==monsterArray[i].y&&forNextPoint.x==monsterArray[i].x)
			shootMonster = monsterArray[i];
		//将怪物血量减一
		shootMonster.Blood--;
		
		//判断如果怪物死亡将怪物变为地板
		if(shootMonster.Blood==0){
			clearInterval(shootMonster.MOVEACTION);
			//将数组中当前后面的怪物以后的怪物下标逐个减一
			for(var i = shootMonster.Index+1;i<monsterArray.length;i++)
				monsterArray[i].Index--;
			monsterArray.splice(shootMonster.Index,1);
			setINFO(HERO.Blood,gameLevel,monsterArray.length);
			getByXY(shootMonster.x,shootMonster.y).src = "img/gameViewIMG/flood2.jpg";
			maps[gameLevel][shootMonster.x][shootMonster.y] = 2;
		}
	
	//判断怪物是否打完判断是否进入下一个关卡
	if(monsterArray.length==0){
		if(confirm("胜利,是否进入下一关")){
			++gameLevel;
			if(!(gameLevel>=maps.length)){
				initMap();
				HERO.name = heroName;
				document.getElementById("mainView").innerHTML="";
				clearAll();
				setMainMap(maps[gameLevel],heroName);
			}else{
				alert("恭喜完成所有关卡！");
				document.getElementById("mainView").style.display="none";
				INFODIV.style.display="none";
				$("#startView").slideDown(500);
			}
		}else{
			initMap();
		
			document.getElementById("mainView").style.display="none";
			INFODIV.style.display="none";
			$("#startView").slideDown(500);
			selectHeroVideo.pause();
			startVideo.play();
		}
	}
}

function clearAll(){
		for(var i = 0;i<monsterFireBallArray.length;i++){
			clearInterval(monsterFireBallArray[i].interval);
		}
		for(var i = 0;i<userFireBallArray.length;i++){
			clearInterval(monsterFireBallArray[i].interval);
		}
		for(var i = 0;i<monsterArray.length;i++){
			clearInterval(monsterArray[i].MOVEACTION);
		}
		monsterFireBallArray.splice(0,monsterFireBallArray.length);
		userFireBallArray.splice(0,userFireBallArray.length);
		monsterArray.splice(0,monsterArray.length);
}